<?php
/**
 * 基础操作——改
 */
class ChangeCTR extends Controller
{
    private $config;
    function init()
    {
        $ConfigModel = new Model('config');
        $config = $ConfigModel->fetch();
        $this->config = $config;
        $this->assign('config', $config);
    }

    function passwd()
    {
        if (Session::get('user')) {
            $tip = '';
            if ($this->isPost()) {
                $pass = md5($_POST['passwd']);
                $npass = md5($_POST['newpass']);
                $rnpass = md5($_POST['renewpass']);
                if (!empty($pass) && !empty($npass) && !empty($rnpass)) {
                    if ($npass == $rnpass) {
                        $UserModel = new Model('user');
                        $user = Session::get('user');
                        if ($user['pass'] === $pass) {
                            if ($UserModel->update(array('pass' => $npass), array('id' => $user['id']))) {
                                Session::del('user');
                                $this->success('修改成功，请重新登录', $this->urlCreate('user', 'login'));
                            } else {
                                $tip = '修改失败，请重试';
                            }
                        } else {
                            $tip = '原始密码错误';
                        }
                    } else {
                        $tip = '两次新密码不一致';
                    }
                } else {
                    $tip = '请完整填写各项';
                }
            }
            $this->assign('tip', $tip);
            $this->assign('title', '修改密码 - ' . $this->config['name']);
            $this->display();
        } else {
            $this->error('非法操作', $this->urlCreate('index', 'index'));
        }
    }

    function infomation()
    {
        if (Session::get('user')) {
            $tip = '';
            $UserModel = new Model('user');
            $user = Session::get('user');
            if ($this->isPost()) {
                $email = empty($_POST['email']) ? '不告诉你' : $_POST['email'];
                $qq = empty($_POST['qq']) ? '不告诉你' : $_POST['qq'];
                $tel = empty($_POST['tel']) ? '不告诉你' : $_POST['tel'];
                if ($UserModel->update(array(
                    'email' => $email,
                    'qq' => $qq,
                    'tell' => $tel), array('id' => $user['id']))) {
                    $user['email'] = $email;
                    $user['qq'] = $qq;
                    $user['tell'] = $tel;
                    Session::del('user');
                    Session::set('user', $user);
                    $this->success('修改成功', $this->urlCreate('user', 'home'));
                } else {
                    $tip = '修改失败，或许是您没有做改动';
                }
            }
            $this->assign('userinfo', $user);
            $this->assign('tip', $tip);
            $this->assign('title', '修改资料 - ' . $this->config['name']);
            $this->display();
        } else {
            $this->error('非法操作', $this->urlCreate('index', 'index'));
        }
    }

    function signature()
    {
        if (Session::get('user')) {
            $tip = '';
            if ($this->isPost()) {
                $signature = $_POST['signature'];
                if (!empty($signature)) {
                    $UserModel = new Model('user');
                    $user = Session::get('user');
                    if ($UserModel->update(array('signature' => $signature), array('id' => $user['id']))) {
                        $this->success('修改成功', $this->urlCreate('user', 'home'));
                    } else {
                        $tip = '修改失败，请重试';
                    }
                } else {
                    $tip = '请完整填写各项';
                }
            }
            $this->assign('tip', $tip);
            $this->assign('title', '修改签名 - ' . $this->config['name']);
            $this->display();
        } else {
            $this->error('非法操作', $this->urlCreate('index', 'index'));
        }
    }
}

?>